var prefix = ctx + "manage/adRecord";

$(function () {
    var options = {
        url: prefix + "/list",
        queryParams: queryParams,
        columns: [
            {
                checkbox: true
            },
            {
                field: 'usId',
                title: '用户ID'
            },
            {
                field: 'usNickname',
                title: '用户名'
            },
            {
                field: 'arRealityMoney',
                title: '广告金币',
                formatter: function (value, row, index) {
                    var valueResult = (value / 10000);
                    return value + '<span class="text-danger">≈' + valueResult + '元</span>';
                }
            },
            {
                field: 'arChannel',
                title: '广告渠道',
                formatter: function (value, row, index) {
                    if (value === 'ad_pangle') {
                        return '穿山甲';
                    }
                    if (value === 'ad_ks') {
                        return '快手';
                    }
                    if (value === 'ad_gdt') {
                        return '优量汇';
                    }
                    if (value === 'ad_other') {
                        return '其他';
                    }
                }
            },
            {
                field: 'uvChannel',
                title: '渠道包',
                formatter: function (value, row, index) {
                    // 遍历所有选项
                    var textValue = "";
                    allOptionsChannel.each(function () {
                        if ($(this).val() === value || $(this).val() === value + "") {
                            textValue = $(this).text();
                        }
                    });
                    return textValue;
                }
            },
            {
                field: 'arCallback',
                title: '是否回调',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return '<span class="badge badge-primary">是</span>';
                    }
                    if (value === 2) {
                        return '<span class="badge badge-secondary">否</span>';
                    }
                }
            },
            {
                field: 'createTime',
                title: '创建时间'
            },
            {
                field: 'usKickback',
                title: '佣金比例',
                formatter: function (value, row, index) {
                    return value + "%";
                }
            },
            {
                field: 'usMoney',
                title: '金币'
            },
            {
                field: 'usParentId',
                title: '代理ID'
            },
            {
                field: 'usParentMoney',
                title: '代理金币'
            },
            {
                field: 'usParentKickback',
                title: '代理佣金比例',
                formatter: function (value, row, index) {
                    if(value){
                        return value + "%";
                    }
                }
            },
            {
                field: 'arMediationRit',
                title: '广告位'
            },
            {
                field: 'arPrimeRit',
                title: '媒体位'
            }
        ]
    };
    $.ryTable.init(options);
    var createTime = $('#createTime').val();
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    _ajax_query(prefix + "/statistics", {
        usId: $("#usId").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    }, callback);
});

/*奖励记录-搜索*/
function search() {
    var createTime = $('#createTime').val();
    if(!createTime){
        $.modalAlert("请选择时间", modal_status.FAIL);
        return;
    }
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    $('.bootstrap-table').bootstrapTable('refresh', queryParams);
    var params = {
        // 传递参数查询参数
        arChannel: $("#arChannel option:selected").val(),
        uvChannel: $("#uvChannel option:selected").val(),
        usId: $("#usId").val(),
        usParentId: $("#usParentId").val(),
        usNickname: $("#usNickname").val(),
        startGoldCoin: $("#startGoldCoin").val(),
        endGoldCoin: $("#endGoldCoin").val(),
        arMediationRit: $("#arMediationRit").val(),
        arCallback: $("#arCallback").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    };
    _ajax_query(prefix + "/statistics", params, callback);
}

function queryParams(params) {
    var createTime = $('#createTime').val();
    var startCreatTime = createTime+" 00:00:00";
    var endTime = createTime+" 23:59:59";
    return {
        // 传递参数查询参数
        pageSize: params.limit,
        pageNum: params.offset / params.limit + 1,
        arChannel: $("#arChannel option:selected").val(),
        uvChannel: $("#uvChannel option:selected").val(),
        usId: $("#usId").val(),
        usParentId: $("#usParentId").val(),
        usNickname: $("#usNickname").val(),
        startGoldCoin: $("#startGoldCoin").val(),
        endGoldCoin: $("#endGoldCoin").val(),
        arMediationRit: $("#arMediationRit").val(),
        arCallback: $("#arCallback").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    };
}

function callback(result) {
    var arRealityMoneySum = result.arRealityMoneySum / 10000;
    var usMoneySum = result.usMoneySum / 10000;
    var usParentMoney = result.usParentMoneySum / 10000;
    $('label#arRealityMoneySum').html("广告金币：" + formatNumber(result.arRealityMoneySum) + "<span class='text-danger'>≈" + arRealityMoneySum + "元</span>");
    $('label#usMoneySum').html("用户金币：" + formatNumber(result.usMoneySum)  + "<span class='text-danger'>≈" + usMoneySum + "元</span>");
    $('label#usParentMoneySum').html("代理佣金：" + formatNumber(result.usParentMoneySum)  + "<span class='text-danger'>≈" + usParentMoney + "元</span>");
}





